//
//  CounterLogsViewController.m
//  GoodsCountingCounter
//
//  Created by iOSdev on 2024/7/23.
//

#import "CounterLogsViewController.h"

#import "UDSaveCountData.h"
#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS
#import "Masonry.h"
#import "NSAttributedString+SJMake.h"
#import "Colours.h"

@interface CounterLogsViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSArray *historyList;
@property (nonatomic, strong) UITableView *tableView;


@end

@implementation CounterLogsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"Goods Counter Logs";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
                                              initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
                                              target:self
                                              action:@selector(clickedEventClear)];
    
    UIImageView *bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mainBG"]];
    [self.view addSubview:bgImage];
    [bgImage makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    // 初始化tableView
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = UIColor.clearColor;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 44.0; // 初始估计高度，这个值可以调整
    [self.view addSubview:self.tableView];
    
    // 创建一个 UIImageView 并设置图片
    UIImage *backgroundImage = [UIImage imageNamed:@"bg"];
    UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
    // 将 UIImageView 设置为 UITableView 的背景视图
    self.tableView.backgroundView = backgroundImageView;
    // 如果你希望背景视图填充整个 tableView，可以设置其自动布局约束
    backgroundImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // 初始化数据源
    self.historyList = [NSArray arrayWithArray:[UDSaveCountData getAllCountData]];
    [self.tableView reloadData];
}


#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1; // 假设只有一个段落
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return 80.0;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.historyList.count; // 返回数据源数组中的元素数量
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    
    // 配置单元格

    cell.textLabel.minimumScaleFactor = 0.6;
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    
    NSString *string = self.historyList[indexPath.row];
    NSArray *list = [string componentsSeparatedByString:@"\n"];
    
    NSAttributedString *attr = [NSAttributedString sj_UIKitText:^(id<SJUIKitTextMakerProtocol>  _Nonnull make) {
        make.font([UIFont systemFontOfSize:12]).textColor(UIColor.blackColor).lineSpacing(8);
        
        make.append(list[0]).font([UIFont systemFontOfSize:14]);
        make.append(@"\n");
        if ([string containsString:@"Single count"]) {
            make.append(list[1]).textColor(UIColor.hollyGreenColor).font([UIFont boldSystemFontOfSize:20]);
        }
        else if ([string containsString:@"Tow count"]) {
            make.append(list[1]).textColor(UIColor.orangeColor).font([UIFont boldSystemFontOfSize:20]);
        }
        else{
            make.append(list[1]).textColor(UIColor.redColor).font([UIFont boldSystemFontOfSize:20]);
        }
        make.append(@"\n");
        make.append(list[2]).textColor(UIColor.grayColor);
    }];
    cell.textLabel.attributedText = attr;
    return cell;
}

- (void)clickedEventClear {
    UIAlertController *alertController = [UIAlertController
                                          alertControllerWithTitle:@"Alert"
                                                           message:@"The history goods count will be deleted"
                                                       preferredStyle:UIAlertControllerStyleAlert];

   UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"OK"
                                                           style:UIAlertActionStyleDefault
                                                        handler:^(UIAlertAction *action) {
       [UDSaveCountData clearCountHistoryData];
       [self viewWillAppear:YES];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
                                                               style:UIAlertActionStyleCancel
                                                             handler:^(UIAlertAction *action) {
                                                             
     }];
    
    [alertController addAction:confirmAction];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

@end
